package my.service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

public class myService extends Service {

	private static final int NOTIFICATION = R.string.app_name;
	private final IBinder binder = new MyBinder();
	private NotificationManager mNM;

	Timer timer = new Timer();
	TimerTask task ;
	
	private Handler handler;
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return binder;
	}

	@Override
	  public void onCreate() {
	    super.onCreate();
	    Log.i("Timer", "Service creating");
	    handler = new Handler(Looper.getMainLooper());
	 
	    
	    timer = new Timer("TweetCollectorTimer");
	    timer.schedule(new TimerTask() {
			@Override
			public void run() {	handler.post(new Runnable() {public void run() {Toast.makeText(getApplicationContext(), "I poop on you", Toast.LENGTH_SHORT).show();}});}
		}, 0, 3000);
	    
	    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Display a notification about us starting.  We put an icon in the status bar.
        showNotification();
	}
	
	public class MyBinder extends Binder {
		myService getService() {
			return myService.this;
		}
	}
	
	private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = "Service is started";

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        Intent main = new Intent(this, MainActivity.class); 
        main.putExtra("Service", "myService");
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, main, 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, "MyService",
                       text, contentIntent);

        // Send the notification.
        mNM.notify(NOTIFICATION, notification);
    }

}
